package com.tvunetworks.center.device.controller;

import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.device.annotation.Logined;
import com.tvunetworks.center.device.model.DeviceGeoRecordFavorite;
import com.tvunetworks.center.device.model.Favorite;
import com.tvunetworks.center.device.model.param.DeviceGeoRecordParam;
import com.tvunetworks.center.device.model.param.FavoriteParam;
import com.tvunetworks.center.device.service.DeviceGeoRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
/**
 * @program: tvucc-instance
 * @description: UserServiceImpl
 * @author lebronchen
 * @create: 2019-02-28 16:53
 **/
@Slf4j
@RestController
@RequestMapping("/favorite")
public class DeviceGeoFavoriterController {
    @Autowired
    private DeviceGeoRecordService deviceGeoRecordService;

    /**
     * 设置喜欢的节目
     * @param loginUser  loginUser
     * @param favoriteParam favoriteParam
     * @return Result
     */
    @PostMapping("/addFavorite")
    public Result addFavorite(@Logined LoginUser loginUser, @RequestBody FavoriteParam favoriteParam){
        if(loginUser==null){
            return ResultUtil.noLogin();
        }
        if(StringUtils.isEmpty(favoriteParam.getName())){
            return ResultUtil.paramError("favoriName can not be null or empty");
        }
        if(StringUtils.isEmpty(favoriteParam.getPeerId())){
            return ResultUtil.paramError("PeerId can not be null or empty");
        }
        if(StringUtils.isEmpty(favoriteParam.getStartTime())){
            return ResultUtil.paramError("StartTime can not be null or empty");
        }
        if(StringUtils.isEmpty(favoriteParam.getEndTime())){
            return ResultUtil.paramError("getEndTime can not be null or empty");
        }
        try {
            favoriteParam.setUserId(loginUser.getId());
            FavoriteParam favoriteQurtyParam =  new FavoriteParam();
            favoriteQurtyParam.setUserId(loginUser.getId());
            favoriteQurtyParam.setName(favoriteParam.getName());
            PageInfo<Favorite> favoritePageInfo = deviceGeoRecordService.listFavirote(favoriteQurtyParam);
            if (favoritePageInfo !=null && favoritePageInfo.getList().size()>0){
                return ResultUtil.error("80600700","Name exist, please change a name.");
            }
            deviceGeoRecordService.addFavorite(favoriteParam);
        } catch (Exception e) {
            log.error("addFavorite Exception",e);
            return ResultUtil.exception(e.getMessage());
        }
        return ResultUtil.success();
    }

    /**
     * 查询喜欢的节目
     * @param loginUser loginUser
     * @param favoriteParam favoriteParam
     * @return Result
     */
    @PostMapping("/listFavorite")
    public Result listFavorite(@Logined LoginUser loginUser, @RequestBody FavoriteParam favoriteParam){
        if(loginUser==null){
            return ResultUtil.noLogin();
        }
        PageInfo<Favorite> list = null;
        try {
            favoriteParam.setUserId(loginUser.getId());
            list = deviceGeoRecordService.listFavirote(favoriteParam);
        } catch (Exception e) {
            log.error("listFavirote Exception",e);
            list = new PageInfo<Favorite>();
        }
        return ResultUtil.success(list);
    }

    /**
     *
     * @param loginUser loginUser
     * @param deviceGeoRecordParam deviceGeoRecordParam
     * @return Result
     */
    @PostMapping("/listGeoRecordFavorite")
    public Result listGeoRecordFavorite(@Logined LoginUser loginUser, @RequestBody DeviceGeoRecordParam deviceGeoRecordParam){
        List<DeviceGeoRecordFavorite> list = null;
//        DeviceGeoRecordParam deviceGeoRecordParam = JSONObject.parseObject(queryData,DeviceGeoRecordParam.class);
        try {
            if(loginUser ==null){
                return ResultUtil.noLogin();
            }
            deviceGeoRecordParam.setUserId(loginUser.getId());
            list = deviceGeoRecordService.listRecordFavorite(deviceGeoRecordParam);
        } catch (Exception e) {
            list = new ArrayList<>();
            log.error("listRecordFavorite.list Exception:[{}]",e);
        }
        return ResultUtil.success(list);
    }

    /**
     * 取消喜欢的节目
     * @param loginUser loginUser
     * @param favoriteParam favoriteParam
     * @return Result
     */
    @PostMapping("/delFavorite")
    public Result delFavorite(@Logined LoginUser loginUser, @RequestBody FavoriteParam favoriteParam){
        if(loginUser==null){
            return ResultUtil.noLogin();
        }
        if(StringUtils.isEmpty(favoriteParam.getId())){
            return ResultUtil.paramError("Id can not be null or empty");
        }

        try {
//            favoriteParam.setUserId(loginUser.getId());
            deviceGeoRecordService.delFavorite(favoriteParam);
        } catch (Exception e) {
            log.error("delFavorite Exception",e);
            return ResultUtil.exception(e.getMessage());
        }
        return ResultUtil.success();
    }

}
